using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;


namespace LMHTX.Modules.DIRECTORY.TEAM.Components
{
	public partial class Team
	{
		#region Private members.
		
		protected int _ID;
		protected Guid _ModuleID = Guid.Empty;
		protected int _AreaID;
		protected int _CareerID;
		protected int _TypeID;
		protected int _TrainingID;
		protected string _Title = string.Empty;
		protected string _TeamLeader = string.Empty;
		protected int _TeamMember;
		protected int _LabourNum;
		protected double _CharterCapital;
		protected double _WorkingCapital;
		protected double _FixedCapital;
		protected double _Turnover;
		protected double _Profit;
		protected string _Comment = string.Empty;
		protected DateTime _CreatedDate = new DateTime(1753, 1, 1);
		protected string _CreatedBy = string.Empty;
		protected DateTime _ApprovedDate = new DateTime(1753, 1, 1);
		protected string _ApprovedBy = string.Empty;

		#endregion
		
		//---------------------------------------------------------------------------------------------

		#region Properties.
		
		public int ID
		{
			set {this._ID = value;}
			get {return this._ID;}
		}
		
		public Guid ModuleID
		{
			set {this._ModuleID = value;}
			get {return this._ModuleID;}
		}
		
		public int AreaID
		{
			set {this._AreaID = value;}
			get {return this._AreaID;}
		}
		
		public int CareerID
		{
			set {this._CareerID = value;}
			get {return this._CareerID;}
		}
		
		public int TypeID
		{
			set {this._TypeID = value;}
			get {return this._TypeID;}
		}
		
		public int TrainingID
		{
			set {this._TrainingID = value;}
			get {return this._TrainingID;}
		}
		
		public string Title
		{
			set {this._Title = value;}
			get {return this._Title;}
		}
		
		public string TeamLeader
		{
			set {this._TeamLeader = value;}
			get {return this._TeamLeader;}
		}
		
		public int TeamMember
		{
			set {this._TeamMember = value;}
			get {return this._TeamMember;}
		}
		
		public int LabourNum
		{
			set {this._LabourNum = value;}
			get {return this._LabourNum;}
		}
		
		public double CharterCapital
		{
			set {this._CharterCapital = value;}
			get {return this._CharterCapital;}
		}
		
		public double WorkingCapital
		{
			set {this._WorkingCapital = value;}
			get {return this._WorkingCapital;}
		}
		
		public double FixedCapital
		{
			set {this._FixedCapital = value;}
			get {return this._FixedCapital;}
		}
		
		public double Turnover
		{
			set {this._Turnover = value;}
			get {return this._Turnover;}
		}
		
		public double Profit
		{
			set {this._Profit = value;}
			get {return this._Profit;}
		}
		
		public string Comment
		{
			set {this._Comment = value;}
			get {return this._Comment;}
		}
		
		public DateTime CreatedDate
		{
			set {this._CreatedDate = value;}
			get {return this._CreatedDate;}
		}
		
		public string CreatedBy
		{
			set {this._CreatedBy = value;}
			get {return this._CreatedBy;}
		}
		
		public DateTime ApprovedDate
		{
			set {this._ApprovedDate = value;}
			get {return this._ApprovedDate;}
		}
		
		public string ApprovedBy
		{
			set {this._ApprovedBy = value;}
			get {return this._ApprovedBy;}
		}
		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static Team Load(int id)
		{
			const string spName = "[dbo].[p_COOPERATIVE_Team_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, id);
			Team entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new Team();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
				if (!reader.IsDBNull(reader.GetOrdinal("AreaID"))) entity.AreaID = reader.GetInt32(reader.GetOrdinal("AreaID"));
				if (!reader.IsDBNull(reader.GetOrdinal("CareerID"))) entity.CareerID = reader.GetInt32(reader.GetOrdinal("CareerID"));
				if (!reader.IsDBNull(reader.GetOrdinal("TypeID"))) entity.TypeID = reader.GetInt32(reader.GetOrdinal("TypeID"));
				if (!reader.IsDBNull(reader.GetOrdinal("TrainingID"))) entity.TrainingID = reader.GetInt32(reader.GetOrdinal("TrainingID"));
				if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
				if (!reader.IsDBNull(reader.GetOrdinal("TeamLeader"))) entity.TeamLeader = reader.GetString(reader.GetOrdinal("TeamLeader"));
				if (!reader.IsDBNull(reader.GetOrdinal("TeamMember"))) entity.TeamMember = reader.GetInt32(reader.GetOrdinal("TeamMember"));
				if (!reader.IsDBNull(reader.GetOrdinal("LabourNum"))) entity.LabourNum = reader.GetInt32(reader.GetOrdinal("LabourNum"));
				if (!reader.IsDBNull(reader.GetOrdinal("CharterCapital"))) entity.CharterCapital = reader.GetDouble(reader.GetOrdinal("CharterCapital"));
				if (!reader.IsDBNull(reader.GetOrdinal("WorkingCapital"))) entity.WorkingCapital = reader.GetDouble(reader.GetOrdinal("WorkingCapital"));
				if (!reader.IsDBNull(reader.GetOrdinal("FixedCapital"))) entity.FixedCapital = reader.GetDouble(reader.GetOrdinal("FixedCapital"));
				if (!reader.IsDBNull(reader.GetOrdinal("Turnover"))) entity.Turnover = reader.GetDouble(reader.GetOrdinal("Turnover"));
				if (!reader.IsDBNull(reader.GetOrdinal("Profit"))) entity.Profit = reader.GetDouble(reader.GetOrdinal("Profit"));
				if (!reader.IsDBNull(reader.GetOrdinal("Comment"))) entity.Comment = reader.GetString(reader.GetOrdinal("Comment"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedDate"))) entity.ApprovedDate = reader.GetDateTime(reader.GetOrdinal("ApprovedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedBy"))) entity.ApprovedBy = reader.GetString(reader.GetOrdinal("ApprovedBy"));
			}
			reader.Close();
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static List<Team> SelectCollectionAll(Guid moduleID)
		{
			List<Team> collection = new List<Team>();
			SqlDataReader reader = (SqlDataReader) SelectReaderAll(moduleID);
			while (reader.Read())
			{
				Team entity = new Team();
				
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
				if (!reader.IsDBNull(reader.GetOrdinal("AreaID"))) entity.AreaID = reader.GetInt32(reader.GetOrdinal("AreaID"));
				if (!reader.IsDBNull(reader.GetOrdinal("CareerID"))) entity.CareerID = reader.GetInt32(reader.GetOrdinal("CareerID"));
				if (!reader.IsDBNull(reader.GetOrdinal("TypeID"))) entity.TypeID = reader.GetInt32(reader.GetOrdinal("TypeID"));
				if (!reader.IsDBNull(reader.GetOrdinal("TrainingID"))) entity.TrainingID = reader.GetInt32(reader.GetOrdinal("TrainingID"));
				if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
				if (!reader.IsDBNull(reader.GetOrdinal("TeamLeader"))) entity.TeamLeader = reader.GetString(reader.GetOrdinal("TeamLeader"));
				if (!reader.IsDBNull(reader.GetOrdinal("TeamMember"))) entity.TeamMember = reader.GetInt32(reader.GetOrdinal("TeamMember"));
				if (!reader.IsDBNull(reader.GetOrdinal("LabourNum"))) entity.LabourNum = reader.GetInt32(reader.GetOrdinal("LabourNum"));
				if (!reader.IsDBNull(reader.GetOrdinal("CharterCapital"))) entity.CharterCapital = reader.GetDouble(reader.GetOrdinal("CharterCapital"));
				if (!reader.IsDBNull(reader.GetOrdinal("WorkingCapital"))) entity.WorkingCapital = reader.GetDouble(reader.GetOrdinal("WorkingCapital"));
				if (!reader.IsDBNull(reader.GetOrdinal("FixedCapital"))) entity.FixedCapital = reader.GetDouble(reader.GetOrdinal("FixedCapital"));
				if (!reader.IsDBNull(reader.GetOrdinal("Turnover"))) entity.Turnover = reader.GetDouble(reader.GetOrdinal("Turnover"));
				if (!reader.IsDBNull(reader.GetOrdinal("Profit"))) entity.Profit = reader.GetDouble(reader.GetOrdinal("Profit"));
				if (!reader.IsDBNull(reader.GetOrdinal("Comment"))) entity.Comment = reader.GetString(reader.GetOrdinal("Comment"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedDate"))) entity.ApprovedDate = reader.GetDateTime(reader.GetOrdinal("ApprovedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedBy"))) entity.ApprovedBy = reader.GetString(reader.GetOrdinal("ApprovedBy"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static List<Team> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			List<Team> collection = new List<Team>();

			SqlDataReader reader = (SqlDataReader) SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				Team entity = new Team();
				
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
				if (!reader.IsDBNull(reader.GetOrdinal("AreaID"))) entity.AreaID = reader.GetInt32(reader.GetOrdinal("AreaID"));
				if (!reader.IsDBNull(reader.GetOrdinal("CareerID"))) entity.CareerID = reader.GetInt32(reader.GetOrdinal("CareerID"));
				if (!reader.IsDBNull(reader.GetOrdinal("TypeID"))) entity.TypeID = reader.GetInt32(reader.GetOrdinal("TypeID"));
				if (!reader.IsDBNull(reader.GetOrdinal("TrainingID"))) entity.TrainingID = reader.GetInt32(reader.GetOrdinal("TrainingID"));
				if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
				if (!reader.IsDBNull(reader.GetOrdinal("TeamLeader"))) entity.TeamLeader = reader.GetString(reader.GetOrdinal("TeamLeader"));
				if (!reader.IsDBNull(reader.GetOrdinal("TeamMember"))) entity.TeamMember = reader.GetInt32(reader.GetOrdinal("TeamMember"));
				if (!reader.IsDBNull(reader.GetOrdinal("LabourNum"))) entity.LabourNum = reader.GetInt32(reader.GetOrdinal("LabourNum"));
				if (!reader.IsDBNull(reader.GetOrdinal("CharterCapital"))) entity.CharterCapital = reader.GetDouble(reader.GetOrdinal("CharterCapital"));
				if (!reader.IsDBNull(reader.GetOrdinal("WorkingCapital"))) entity.WorkingCapital = reader.GetDouble(reader.GetOrdinal("WorkingCapital"));
				if (!reader.IsDBNull(reader.GetOrdinal("FixedCapital"))) entity.FixedCapital = reader.GetDouble(reader.GetOrdinal("FixedCapital"));
				if (!reader.IsDBNull(reader.GetOrdinal("Turnover"))) entity.Turnover = reader.GetDouble(reader.GetOrdinal("Turnover"));
				if (!reader.IsDBNull(reader.GetOrdinal("Profit"))) entity.Profit = reader.GetDouble(reader.GetOrdinal("Profit"));
				if (!reader.IsDBNull(reader.GetOrdinal("Comment"))) entity.Comment = reader.GetString(reader.GetOrdinal("Comment"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedDate"))) entity.ApprovedDate = reader.GetDateTime(reader.GetOrdinal("ApprovedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedBy"))) entity.ApprovedBy = reader.GetString(reader.GetOrdinal("ApprovedBy"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		

		public static DataSet SelectAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_COOPERATIVE_Team_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_COOPERATIVE_Team_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_COOPERATIVE_Team_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_COOPERATIVE_Team_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertTeam(Guid moduleID, int areaID, int careerID, int typeID, int trainingID, string title, string teamLeader, int teamMember, int labourNum, double charterCapital, double workingCapital, double fixedCapital, double turnover, double profit, string comment, DateTime createdDate, string createdBy, DateTime approvedDate, string approvedBy)
		{
			Team entity = new Team();	
			entity.ModuleID = moduleID;
			entity.AreaID = areaID;
			entity.CareerID = careerID;
			entity.TypeID = typeID;
			entity.TrainingID = trainingID;
			entity.Title = title;
			entity.TeamLeader = teamLeader;
			entity.TeamMember = teamMember;
			entity.LabourNum = labourNum;
			entity.CharterCapital = charterCapital;
			entity.WorkingCapital = workingCapital;
			entity.FixedCapital = fixedCapital;
			entity.Turnover = turnover;
			entity.Profit = profit;
			entity.Comment = comment;
			entity.CreatedDate = createdDate;
			entity.CreatedBy = createdBy;
			entity.ApprovedDate = approvedDate;
			entity.ApprovedBy = approvedBy;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_COOPERATIVE_Team_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@AreaID", SqlDbType.Int, AreaID);
			db.AddInParameter(dbCommand, "@CareerID", SqlDbType.Int, CareerID);
			db.AddInParameter(dbCommand, "@TypeID", SqlDbType.Int, TypeID);
			db.AddInParameter(dbCommand, "@TrainingID", SqlDbType.Int, TrainingID);
			db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, Title);
			db.AddInParameter(dbCommand, "@TeamLeader", SqlDbType.NVarChar, TeamLeader);
			db.AddInParameter(dbCommand, "@TeamMember", SqlDbType.Int, TeamMember);
			db.AddInParameter(dbCommand, "@LabourNum", SqlDbType.Int, LabourNum);
			db.AddInParameter(dbCommand, "@CharterCapital", SqlDbType.Float, CharterCapital);
			db.AddInParameter(dbCommand, "@WorkingCapital", SqlDbType.Float, WorkingCapital);
			db.AddInParameter(dbCommand, "@FixedCapital", SqlDbType.Float, FixedCapital);
			db.AddInParameter(dbCommand, "@Turnover", SqlDbType.Float, Turnover);
			db.AddInParameter(dbCommand, "@Profit", SqlDbType.Float, Profit);
			db.AddInParameter(dbCommand, "@Comment", SqlDbType.NVarChar, Comment);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@ApprovedDate", SqlDbType.DateTime, ApprovedDate.Year <= 1753 ? DBNull.Value : (object) ApprovedDate);
			db.AddInParameter(dbCommand, "@ApprovedBy", SqlDbType.NVarChar, ApprovedBy);
			
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				ID = (int) db.GetParameterValue(dbCommand, "@ID");
				return ID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				ID = (int) db.GetParameterValue(dbCommand, "@ID");
				return ID;
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<Team> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Team item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateTeam(int id, Guid moduleID, int areaID, int careerID, int typeID, int trainingID, string title, string teamLeader, int teamMember, int labourNum, double charterCapital, double workingCapital, double fixedCapital, double turnover, double profit, string comment, DateTime createdDate, string createdBy, DateTime approvedDate, string approvedBy)
		{
			Team entity = new Team();			
			entity.ID = id;
			entity.ModuleID = moduleID;
			entity.AreaID = areaID;
			entity.CareerID = careerID;
			entity.TypeID = typeID;
			entity.TrainingID = trainingID;
			entity.Title = title;
			entity.TeamLeader = teamLeader;
			entity.TeamMember = teamMember;
			entity.LabourNum = labourNum;
			entity.CharterCapital = charterCapital;
			entity.WorkingCapital = workingCapital;
			entity.FixedCapital = fixedCapital;
			entity.Turnover = turnover;
			entity.Profit = profit;
			entity.Comment = comment;
			entity.CreatedDate = createdDate;
			entity.CreatedBy = createdBy;
			entity.ApprovedDate = approvedDate;
			entity.ApprovedBy = approvedBy;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_COOPERATIVE_Team_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@AreaID", SqlDbType.Int, AreaID);
			db.AddInParameter(dbCommand, "@CareerID", SqlDbType.Int, CareerID);
			db.AddInParameter(dbCommand, "@TypeID", SqlDbType.Int, TypeID);
			db.AddInParameter(dbCommand, "@TrainingID", SqlDbType.Int, TrainingID);
			db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, Title);
			db.AddInParameter(dbCommand, "@TeamLeader", SqlDbType.NVarChar, TeamLeader);
			db.AddInParameter(dbCommand, "@TeamMember", SqlDbType.Int, TeamMember);
			db.AddInParameter(dbCommand, "@LabourNum", SqlDbType.Int, LabourNum);
			db.AddInParameter(dbCommand, "@CharterCapital", SqlDbType.Float, CharterCapital);
			db.AddInParameter(dbCommand, "@WorkingCapital", SqlDbType.Float, WorkingCapital);
			db.AddInParameter(dbCommand, "@FixedCapital", SqlDbType.Float, FixedCapital);
			db.AddInParameter(dbCommand, "@Turnover", SqlDbType.Float, Turnover);
			db.AddInParameter(dbCommand, "@Profit", SqlDbType.Float, Profit);
			db.AddInParameter(dbCommand, "@Comment", SqlDbType.NVarChar, Comment);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year == 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@ApprovedDate", SqlDbType.DateTime, ApprovedDate.Year == 1753 ? DBNull.Value : (object) ApprovedDate);
			db.AddInParameter(dbCommand, "@ApprovedBy", SqlDbType.NVarChar, ApprovedBy);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<Team> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Team item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateTeam(int id, Guid moduleID, int areaID, int careerID, int typeID, int trainingID, string title, string teamLeader, int teamMember, int labourNum, double charterCapital, double workingCapital, double fixedCapital, double turnover, double profit, string comment, DateTime createdDate, string createdBy, DateTime approvedDate, string approvedBy)
		{
			Team entity = new Team();			
			entity.ID = id;
			entity.ModuleID = moduleID;
			entity.AreaID = areaID;
			entity.CareerID = careerID;
			entity.TypeID = typeID;
			entity.TrainingID = trainingID;
			entity.Title = title;
			entity.TeamLeader = teamLeader;
			entity.TeamMember = teamMember;
			entity.LabourNum = labourNum;
			entity.CharterCapital = charterCapital;
			entity.WorkingCapital = workingCapital;
			entity.FixedCapital = fixedCapital;
			entity.Turnover = turnover;
			entity.Profit = profit;
			entity.Comment = comment;
			entity.CreatedDate = createdDate;
			entity.CreatedBy = createdBy;
			entity.ApprovedDate = approvedDate;
			entity.ApprovedBy = approvedBy;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_COOPERATIVE_Team_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@AreaID", SqlDbType.Int, AreaID);
			db.AddInParameter(dbCommand, "@CareerID", SqlDbType.Int, CareerID);
			db.AddInParameter(dbCommand, "@TypeID", SqlDbType.Int, TypeID);
			db.AddInParameter(dbCommand, "@TrainingID", SqlDbType.Int, TrainingID);
			db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, Title);
			db.AddInParameter(dbCommand, "@TeamLeader", SqlDbType.NVarChar, TeamLeader);
			db.AddInParameter(dbCommand, "@TeamMember", SqlDbType.Int, TeamMember);
			db.AddInParameter(dbCommand, "@LabourNum", SqlDbType.Int, LabourNum);
			db.AddInParameter(dbCommand, "@CharterCapital", SqlDbType.Float, CharterCapital);
			db.AddInParameter(dbCommand, "@WorkingCapital", SqlDbType.Float, WorkingCapital);
			db.AddInParameter(dbCommand, "@FixedCapital", SqlDbType.Float, FixedCapital);
			db.AddInParameter(dbCommand, "@Turnover", SqlDbType.Float, Turnover);
			db.AddInParameter(dbCommand, "@Profit", SqlDbType.Float, Profit);
			db.AddInParameter(dbCommand, "@Comment", SqlDbType.NVarChar, Comment);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year == 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.NVarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@ApprovedDate", SqlDbType.DateTime, ApprovedDate.Year == 1753 ? DBNull.Value : (object) ApprovedDate);
			db.AddInParameter(dbCommand, "@ApprovedBy", SqlDbType.NVarChar, ApprovedBy);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<Team> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Team item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteTeam(int id)
		{
			Team entity = new Team();
			entity.ID = id;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_COOPERATIVE_Team_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<Team> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Team item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}